<template>
  <div>
    <div class="top">
      <el-form ref="form" :model="form" label-width="80px" inline size="mini">
        <el-form-item>
          <el-select v-model="form.earCode" placeholder="选择耳号" clearable>
            <el-option
              :label="item.earCode"
              :value="item.earCode"
              v-for="item in earList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- value-format="yyyy-MM-dd HH:mm:ss"   @change="changeTime"-->
        <el-form-item label="查询时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableList" style="width: 100%" border>
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        width="width"
        v-for="(item, index) in tableHead"
        :key="index"
        align="center"
      >
      </el-table-column>
      <!-- 无数据 -->
      <template slot="empty">
        <div class="noData" v-show="tableList.length === 0">
          <!--  v-show="data.length===0" -->
          <img src="@/assets/images/noData.png" />
          <p>暂无数据</p>
        </div>
      </template>
    </el-table>
    <Pagination :total="total" @pagination="handlePage" :page.sync="form.pageNum"></Pagination>
  </div>
</template>

<script>
import { boarList } from "@/api/production-management/boar.js";
import {
  boarEntryTableHead,
  boarLeaveTableHead,
  boarTransferTableHead,
} from "../constant/constant";
import { entryList, tdList, exitList } from "@/api/event";
export default {
  name: "first",
  inject: ["name"],
  inheritAttrs: false,
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      entryForm: {
        buildingsId: null, //舍
        buildsName: "",
        columnId: null, //栏号
        columnName: "",
        //createTime: "",
        dateOfBirth: "", //出生日期
        earCode: "", //耳号
        entryBirths: 0, //入场胎次
        entryDate: "", //进场日期
        entryType: "", //进场类型
        //id: 0,
        isPresent: "", //是否在场
        location: "", //所在位置（回显）
        operatorsUser: null, //创建人
        operatorsUserName: "",
        pageNum: 0,
        pageSize: 0,
        pigType: "", //猪只类型
        price: 0, //价格
        remark: "", //备注
        state: "", //状态
        stateDate: "", //状态日期
        tdDate: "",
        updateTime: "", //转舍日期
        variety: "", //品种
      },
      form: {},
      time: [],
      earList: [],
      tableList: [],
      tableHead: [],
      total: 0,
    };
  },
  created() {
    this.getBoarList();
    switch (this.name) {
      case "entry":
        this.form = {
          entryPigType: 0,
          earCode: "",
          startEntryTime: "", //开始时间
          endEntryTime: "", //结束时间
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = boarEntryTableHead;
        break;
      case "leave":
        this.form = {
          exitPigType: 0,
          earCode: "",
          ids: [],
          startExitTime: "",
          endExitTime: "",
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = boarLeaveTableHead;
        break;
      case "transfer":
        this.form = {
          tdPigType: 0,
          earCode: "",
          ids: [],
          startTdTime: "",
          endTdTime: "",
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = boarTransferTableHead;
        break;
    }
    this.getList();
  },
  methods: {
    async getBoarList() {
      this.name === "leave"
        ? (this.entryForm.isPresent = "离场")
        : (this.entryForm.isPresent = "");
      let res = await boarList(this.entryForm);
      if (res.code == 200) {
        this.earList = res.data.list;
      }
    },
    async getList() {
      //console.log('我输出了')
      let method = entryList;
      switch (this.name) {
        case "entry":
          method = entryList;
          break;
        case "leave":
          method = exitList;
          break;
        case "transfer":
          method = tdList;
          break;
      }
      let res = await method(this.form);
      if (res.code == 200) {
        this.tableList = res.data.list;
        this.total = res.data.total;
      }
    },
    searchList() {
      this.form.pageNum = 1;
      this.getList();
    },
    handlePage(val) {
      this.form.pageNum = val.page;
      this.form.pageSize = val.limit;
      this.getList();
    },
  },
  watch: {
    time(newVal) {
      if (newVal) {
        switch (this.name) {
          case "entry":
            this.form.startEntryTime = newVal[0];
            this.form.endEntryTime = newVal[1];
            break;
          case "leave":
            this.form.startExitTime = newVal[0];
            this.form.endExitTime = newVal[1];
            break;
          case "transfer":
            this.form.startTdTime = newVal[0];
            this.form.endTdTime = newVal[1];
            break;
        }
      } else {
        this.time = [];
      }
    },
  },
};
</script>

<style></style>
